package demo1;

import java.util.HashMap;
import java.util.Map;

/**
 * 和为 K 的子数组
 */

public class LeetCode560 {
    public int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> hash = new HashMap<>();
        hash.put(0, 1);
        int sum = 0, ret = 0;
        for (int x : nums) {
            sum += x;
            ret += hash.getOrDefault(sum - k, 0);
            hash.put(sum, hash.getOrDefault(sum, 0) + 1);  //要放在下面
        }
        return ret;
    }
}
